In this article I will explain how to bind dropdownlist using json data in asp.net with an example. On page load it attends to featch AJAX data from URL. Upon success, it fills product items based on the ajax call’s result.
HTML page:
<select id="cboProduct">
</select>
$(document).ready(function () {
$(".errorBlockMain").hide();
$.ajax({
type: 'POST',
url: '@Url.Action("BindProduct ", "Products", new { area = "Admin" })',
data: "{}",
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (data) {
$('#cboProduct').empty();
var inc = 0;
$.each(data, function (value) {
$("[id$='cboProduct']").append($("<option></option>").val(data[inc].ProductId).html(data[inc].Name));
inc = inc + 1;
});
},
error: function (xhr, ajaxOptions, thrownError) {
$(".errorBlockMain").html(xhr.responseText);
$(".errorBlockMain").show();
}
});
});
server Side:
[HttpPost]
public JsonResult BindProduct()
{
ProductItems mProductItems = (ProductItems) ProductItems.LoadListProduct ((string)HttpContext.Session["DBConnector"], "SELECT * FROM Product order by Product Idasc");
return Json(mProductItems, JsonRequestBehavior.AllowGet);
}
Post your comments / questions
Recent Article
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
- FieldError: Cannot resolve keyword 'id' into field in Django project
- How to hide the ID field from the Django admin?
- It is impossible to add a non nullable field without specifying a default. Django error
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
Related Article